/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihome.devices;

import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.io.IOException;

/**
 *
 * @author debian
 */
public class DeviceController{

    static com.pi4j.io.gpio.GpioController gpio = GpioFactory.getInstance();
    static GpioPinDigitalOutput fanPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "MyFan", PinState.LOW);
    static GpioPinDigitalOutput heatPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "MyHeater", PinState.LOW);
    
    public DeviceController() {
    }
    
    public void turnAircoOn()
    {
        fanPin.high();
    }
    
    public void turnAircoOff()
    {
        fanPin.low();
    }
    
    public void turnOnHeater()
    {
        heatPin.high();
    }
    
    public void turnOffHeater()
    {
        heatPin.low();
    }
    
     public void takePicture(String pFileName)
    {
        executeShellCommand("raspistill -o "+pFileName+".jpg");
        executeShellCommand("sudo mv -f "+pFileName+".jpg ../../../../../../var/www/");
    }
   
    public void film(String pFileName,long pTimeInMilliSeconds)
    {
        if(pTimeInMilliSeconds > 5001)
            pTimeInMilliSeconds = 5000;
        executeShellCommand("raspivid -o "+pFileName+".h264 -t "+pTimeInMilliSeconds);
    }
   
    private void executeShellCommand(String pCommand)
    {
        try {
            Runtime run = Runtime.getRuntime() ;
            Process pr = run.exec(pCommand) ;
            pr.waitFor() ;
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}